package com.mayp.supermarket.sales;

import com.mayp.supermarket.sales.policy.DiscountStrategy;
import com.mayp.supermarket.sales.policy.Fruit;
import com.mayp.supermarket.sales.policy.PricingStrategy;
import com.mayp.supermarket.sales.policy.PromotionStrategy;
import com.mayp.supermarket.sales.variety.Apple;
import com.mayp.supermarket.sales.variety.Mango;
import com.mayp.supermarket.sales.variety.Strawberry;

import java.util.Objects;

/**
 * @Description : 超市类
 * @Author : mayp
 * @Date: 2024/7/12  11:22
 */
public class Supermarket {
    /**
     * 计算总价格
     * @param fruits
     * @param strategy
     * @return
     */
    public static double calculateTotalPrice(Fruit[] fruits, PricingStrategy strategy) {
        double totalPrice = 0.0;
        // 计算全部斤数价格
        for (Fruit fruit : fruits) {
            totalPrice += fruit.calculateTotalPrice();
        }
        // 如果没有促销价格时，直接返回总价
        if (Objects.isNull(strategy)) {
            return totalPrice;
        }
        // 返回折扣
        return strategy.applyDiscount(totalPrice);
    }

    public static void main(String[] args) {
        // 实例化促销策略
        PricingStrategy strawberryDiscount = new DiscountStrategy(0.8); // 草莓8折
        PricingStrategy promotion = new PromotionStrategy(100, 10); // 满百减十

        // 计算顾客A的总价（无促销）
        Fruit[] fruitsA = {new Apple(3), new Strawberry(5)};
        double totalPriceA = Supermarket.calculateTotalPrice(fruitsA, null);
        System.out.println("顾客A购买商品的总价为: " + totalPriceA + "元");

        // 计算顾客B的总价（无促销）
        Fruit[] fruitsB = {new Apple(2), new Strawberry(3), new Mango(4)};
        double totalPriceB = Supermarket.calculateTotalPrice(fruitsB, null);
        System.out.println("顾客B购买商品的总价为: " + totalPriceB + "元");

        // 计算顾客C的总价（草莓8折）
        Fruit[] fruitsC = {new Apple(1), new Strawberry(2), new Mango(3)};
        double totalPriceC = Supermarket.calculateTotalPrice(fruitsC, strawberryDiscount);
        System.out.println("顾客C购买商品的总价为（含草莓折扣）: " + totalPriceC + "元");

        // 计算顾客D的总价（草莓8折 + 满百减十）
        Fruit[] fruitsD = {new Apple(3), new Strawberry(2), new Mango(4)};
        double totalPriceD = Supermarket.calculateTotalPrice(fruitsD, promotion);
        System.out.println("顾客D购买商品的总价为（含草莓折扣和满100减10促销）: " + totalPriceD + "元");
    }
}
